package com.sk.bilibili.model.fragment.main;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.bean.History;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.dto.Page;
import com.sk.bilibili.mapper.FocusMapper;
import com.sk.bilibili.mapper.HistoryMapper;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.view.fragment.main.HistoryFragment;

public class HistoryModel extends BaseModel<HistoryFragment> {

    private Integer type;
    private int page=1;
    private int size=10;
    @Autowired
    HistoryMapper historyMapper;
    @Autowired
    FocusMapper focusMapper;

    public void refresh() {
        page=1;
        JsonMap map=getJsonMap();
        RequestUtils.process(historyMapper.pageSearch(map), historyPage -> {
            view.refreshSuccess(historyPage.getRecords());
            page++;
        });
    }

    @Override
    public void loadData() {
        JsonMap map = getJsonMap();
        RequestUtils.process(historyMapper.pageSearch(map), historyPage -> {
            view.loadMoreSuccess(historyPage.getRecords());
            page++;
        });
    }

    private JsonMap getJsonMap() {
        JsonMap map=new JsonMap();
        map.put("type",type);
        map.put("page",page);
        map.put("size",size);
        return map;
    }

    public void setType(Integer type) {
        this.type=type;
        refresh();
    }

    public void switchFocus(Integer focusId) {
        RequestUtils.process(focusMapper.switchFocus(focusId),
                focus -> view.switchFocusSuccess(focus));
    }

}